package com.ztesoft.web.baseconfig.db.dao;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.web.baseconfig.db.arg.DmPlanRelationArg;
import com.ztesoft.web.baseconfig.db.arg.DmPlanRelationArg.DmPlanRelationCriteria;
import com.ztesoft.web.baseconfig.db.dao.mapper.IDmPlanRelationMapper;
import com.ztesoft.web.baseconfig.db.po.DmPlanRelationPO;

@Repository
public class DmPlanRelationDao extends SqlSessionDaoSupport {

	@Resource(name = "majorSqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	public List<Integer> countByArg(DmPlanRelationArg arg) {
		return getMapper().countByArg(arg);
	}

	public int deleteByArg(DmPlanRelationArg arg) {
		return getMapper().deleteByArg(arg);
	}

	public List<DmPlanRelationPO> selectByArg(DmPlanRelationArg arg) {
		return getMapper().selectByArg(arg);
	}

	/**
	 * 根据组号，查询计划策略关系
	 * 
	 * @param record
	 * @return
	 */
	public List<DmPlanRelationPO> selectByGroupNbr(DmPlanRelationPO record) {
		return getMapper().selectByGroupNbr(record);
	}

	/**
	 * 查找计划下的策略组
	 * 
	 * @param dmPlanId
	 * @return
	 */
	public List<DmPlanRelationPO> selectGroupByDmPlan(Integer dmPlanId) {
		return getMapper().selectGroupByDmPlan(dmPlanId);
	}

	public int updateByArgSelective(DmPlanRelationPO record, DmPlanRelationArg arg) {
		return getMapper().updateByArgSelective(record, arg);
	}

	public int updateByArg(DmPlanRelationPO record, DmPlanRelationArg arg) {
		return getMapper().updateByArg(record, arg);
	}

	public Page<DmPlanRelationPO> selectByArgAndPage(DmPlanRelationArg arg, Page<DmPlanRelationPO> resultPage) {
		List<DmPlanRelationPO> resultList = getMapper().selectByArgAndPage(arg, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public int insert(DmPlanRelationPO record) {
		return getMapper().insert(record);
	}

	public int insertSelective(DmPlanRelationPO record) {
		return getMapper().insertSelective(record);
	}

	public int insertBatch(List<DmPlanRelationPO> records) {
		return getMapper().insertBatch(records);
	}

	public int deleteByPrimaryKey(DmPlanRelationPO record) {
		return getMapper().deleteByPrimaryKey(record);
	}

	public DmPlanRelationPO selectByPrimaryKey(DmPlanRelationPO record) {
		return getMapper().selectByPrimaryKey(record);
	}

	/**
	 * 根据策略ID查询计划策略关系对象
	 * 
	 * @param record
	 * @return
	 */
	public DmPlanRelationPO selectByDmStrategyId(DmPlanRelationPO record) {
		return getMapper().selectByDmStrategyId(record);
	}

	public int updateByPrimaryKeySelective(DmPlanRelationPO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(DmPlanRelationPO record) {
		return getMapper().updateByPrimaryKey(record);
	}

	public int updateDToA(Integer dmPlanId) {
		return getMapper().updateDToA(dmPlanId);
	}

	public int updateTo00X(Integer dmPlanId) {
		return getMapper().updateTo00X(dmPlanId);
	}

	public List<DmPlanRelationPO> selectByDmPlanId(Integer dmPlanId) {
		return getMapper().selectByDmPlanId(dmPlanId);
	}

	/**
	 * 获取指定表的已经失效的计划
	 * 
	 * @param sourceDataId
	 * @return
	 */
	public DmPlanRelationPO selectBySourceDataId(Integer sourceDataId) {
		return getMapper().selectBySourceDataId(sourceDataId);
	}

	public List<DmPlanRelationPO> selectByDmPlanId00X(Integer dmPlanId) {
		return getMapper().selectByDmPlanId00X(dmPlanId);
	}

	public List<DmPlanRelationPO> selectByEditPlan(Integer dmPlanId, String groupNbr) {
		return getMapper().selectByEditPlan(dmPlanId, groupNbr);
	}

	/**
	 * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
	 * 
	 * @param params
	 *            Map,Key=字段名，value=查询值
	 * @return
	 */
	public List<DmPlanRelationPO> selectByMap(Map<String, Object> params) {
		return (selectByArg(buildQueryObject(params)));
	}

	private DmPlanRelationArg buildQueryObject(Map<String, Object> params) {

		DmPlanRelationArg arg = new DmPlanRelationArg();
		DmPlanRelationCriteria criteria = arg.createCriteria();

		Class criteriaClass = criteria.getClass();
		Set keys = params.keySet();

		if (keys != null) {

			Iterator iterator = keys.iterator();

			while (iterator.hasNext()) {

				Object key = iterator.next();
				Object value = params.get(key);
				for (Method method : criteriaClass.getMethods()) {
					if (method.getName().equals("and" + StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
						try {
							method.invoke(criteria, value);
						} catch (Exception e) {
							throw new SysRuntimeException(e);
						}
						break;
					}
				}
			}
		}
		return arg;
	}

	public IDmPlanRelationMapper getMapper() {
		return getSqlSession().getMapper(IDmPlanRelationMapper.class);
	}

}
